package com.liqi.display.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @program: redis工具类
 * @description:
 * @author: zlw
 * @create: 2019-12-11 20:52
 **/
@Component
public class RedisUtil {

    public static final long TOKEN_EXPIRES_SECOND = 1800;

    @Autowired
    private StringRedisTemplate redisTpl;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 向redis中设值
     * @param key 使用 a:b:id的形式在使用rdm进行查看redis情况时会看到分层文件夹的展示形式，便于管理
     * @param value
     * @return
     */
    public boolean set(String key, String value) {
        boolean result = false;
        try {
            redisTpl.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 向redis中设置，同时设置过期时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean set(String key, String value, long time) {
        boolean result = false;
        try {
            redisTpl.opsForValue().set(key, value);
            expire(key, time);
            result =  true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取redis中的值
     * @param key
     * @return
     */
    public String get(String key) {
        String result = null;
        try {
            result = redisTpl.opsForValue().get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取redis中的值
     * @param key
     * @return
     */
    public Object getObj(Object key) {
        Object result = null;
        try {
            result = redisTpl.opsForValue().get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 设置key的过期时间
     * @param key
     * @param time
     * @return
     */
    public boolean expire(String key, long time) {
        boolean result = false;
        try {
            if(time > 0) {
                redisTpl.expire(key, time, TimeUnit.SECONDS);
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据key删除对应value
     * @param key
     * @return
     */
    public boolean remove(String key) {
        boolean result = false;
        try {
            redisTpl.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断是否有该key
     * @param key
     * @return
     */
    public boolean hasKey(String key) {
        boolean result = false;
        try {
            result = redisTpl.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 对象缓存
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
    public void setObj(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *
     * @param pkey 父key
     * @param ckey 子key
     * @param value 值
     */
    public void hashSet(String pkey,String ckey, Object value) {
        try {
            redisTemplate.boundHashOps(pkey).put(ckey,value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Object hashGet(String pkey,String ckey) {
        try {
            Object value = redisTemplate.boundHashOps(pkey).get(ckey);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Map hashGetP(String pkey) {
        try {
            BoundHashOperations<String, Object, Object> boundHashMap = redisTemplate.boundHashOps(pkey);
            Map<Object, Object> map = boundHashMap.entries();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Map hashGetEntries(String pkey) {
        try {
            Map map = (Map)redisTemplate.boundHashOps(pkey).entries();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
